package com.mixue.controller;


import com.mixue.model.Category;
import com.mixue.restful.ResoutSet;
import com.mixue.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CatagoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public ResoutSet<String> add(@RequestBody Category category){
        categoryService.save(category);
        System.out.println(category);
        return ResoutSet.sussess("添加成功！");
    }
    @GetMapping("/list")
    public ResoutSet<List<Category>> list(Category category){

        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort);
        List<Category> list=categoryService.list(queryWrapper);
        System.out.println("查到的数据："+list);
        return ResoutSet.sussess(list);
    }
    @GetMapping("/page")
    public ResoutSet<Page> page(int page,int pageSize){
        Page<Category> pageInfo=new Page<>();
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,queryWrapper);
        return ResoutSet.sussess(pageInfo);
    }
    @DeleteMapping
    public ResoutSet<String> delete(Long id){
        System.out.println(id);
        categoryService.removeById(id);
        return ResoutSet.sussess("删除成功");

    }
    @PutMapping
    public ResoutSet<String> update(@RequestBody Category category){
        System.out.println(category);
        categoryService.updateById(category);
        System.out.println("修改成功");
        return ResoutSet.sussess("修改成功！");
    }

}
